package controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import model.Cart;
import model.CartDao;
import model.CartShop;
import model.Shop;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


@WebServlet("/cart")
public class CartServlet extends HttpServlet {

    //使用这个数据类型来将数据转成json格式
    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");




        //接收客户端发来的json数据
        InputStreamReader reader = new InputStreamReader(req.getInputStream(),"UTF-8");
        char[] buff = new char[1024];
        int length = 0;
        String str = "";
        while((length = reader.read(buff))!=-1){
            str = str + new String(buff,0,length);
        }

        reader.close();

        System.out.println("购物车中的信息："+str);
        String strShop = str.split("]",3)[0] + "]";
        String strCount = str.split("]",3)[1] + "]";
        String strUserId = str.split("]",3)[2];



        //解析发来的数据
        JavaType javaType1 = objectMapper.getTypeFactory().constructParametricType(List.class, Shop.class);
        List<Shop> cartShop = objectMapper.readValue(strShop,javaType1);
        JavaType javaType2 = objectMapper.getTypeFactory().constructParametricType(List.class, Integer.class);
        List<Integer> cartCount = objectMapper.readValue(strCount,javaType2);

        String id = strUserId.replace("\"","").replace("\"","");

        int userId = Integer.parseInt(id);


        //将购物车中的信息根据count插入到数据库中
        CartDao cartDao = new CartDao();
        cartDao.insertShopToCart(cartShop,cartCount,userId);





        System.out.println(strShop);
        System.out.println(strCount);
        System.out.println(userId);






    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf8");
        resp.setCharacterEncoding("utf8");
        resp.setContentType("text/html;charset=utf-8");

        String id = req.getParameter("userId");
        int userId = Integer.parseInt(id);
        //在数据库cart表里面查询信息，并通过json格式返回
        CartDao cartDao = new CartDao();
        List<Shop> cartShops = cartDao.getAllShops(userId);
        String respJson = objectMapper.writeValueAsString(cartShops);
        System.out.println(respJson);
        //除了返回商品，还要返回商品的数量，以List<Integer>接收
        List<Integer> curnum = cartDao.selectShopCount(userId);
        String respCurnum = objectMapper.writeValueAsString(curnum);
        System.out.println(respCurnum);

        resp.getWriter().write(respJson);
        resp.getWriter().write(respCurnum);







    }
}












